//
//  LotteryTableView.m
//  BCW
//
//  Created by Mac on 16/3/29.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "LotteryTableView.h"
#import "LotteryCell.h"
#import "TableHeadView.h"
#import "TableFooterView.h"
#import "LotteryCellModel.h"
#import "LotterySectionModel.h"

@interface LotteryTableView()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) TableFooterView   *footerView;

@end

@implementation LotteryTableView

- (TableFooterView *)footerView{
    if (!_footerView) {
        _footerView = [[TableFooterView alloc] initWithFrame:CGRectMake(0, 0, self.width, 30)];
    }
    return _footerView;
}

- (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style{
    self = [super initWithFrame:frame style:style];
    if (self) {
        self.delegate = self;
        self.dataSource = self;
        self.separatorStyle = UITableViewCellSeparatorStyleNone;
        
//        [self registerClass:[TableFooterView class] forHeaderFooterViewReuseIdentifier:@"foot"];
    }
    return self;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *ID = @"cell";
    LotteryCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell) {
        cell = [[NSBundle mainBundle] loadNibNamed:@"LotteryCell" owner:nil options:nil].firstObject;
    }
    
    if (indexPath.row % 2 == 0) {
        cell.viewColor = WhiteColor;
    }
    else{
        cell.viewColor = [UIColor groupTableViewBackgroundColor];
    }
    
    // 取出模型
    LotterySectionModel *sectionModel = _cellModels[indexPath.section];
    cell.model = sectionModel.cellModels[indexPath.row];
    cell.indexStr = [NSString stringWithFormat:@"第%li球", indexPath.row + 1];
    return cell;
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return _cellModels.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    LotterySectionModel *sectionModel = _cellModels[section];
    return sectionModel.cellModels.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 30;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    TableHeadViewType type = TableHeadViewThird;
    
    if (section == 0) {
        type = TableHeadViewFirst;
    }
    
    TableHeadView *headView = [[TableHeadView alloc] initWithType:type];
    headView.lotteryModel = _lotteryModel;
    if (type == TableHeadViewThird) {
        LotterySectionModel *model = _cellModels[section];
        headView.title = model.headTitle;
    }
    return headView;
}

//- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
//    UIView *v = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"foot"];
//    return v;
//}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 5;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
//    switch (section) {
//        case 0:
//        {
    if (section == 0) {
        return 95.f;
    }
    else{
        return 35.f;
    }
    
//        }
//            break;
//        case 1:
//        case 2:
//        case 3:
//        {
//            return 70.f;
//        }
//            break;
//        default:
//        {
//            return 35.f;
//        }
//            break;
//    }
}


@end
